package com.springblog.serviceimpl;


import com.springblog.common.utils.PageUtils;
import com.springblog.entity.JpaEntity;
import com.springblog.mapper.JpaMapper;
import com.springblog.service.JpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class JpaServiceImpl implements JpaService {

    @Autowired
    private JpaMapper jpaMapper;

    @Override
    public PageUtils queryPageByPureJPA(Map<String, Object> params) {

//        Specification<Blog> specification = new Specification<Blog>() {
//            @Override
//            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//                //所有的断言 及条件
//                List<Predicate> predicates = new ArrayList<>();
//                //精确匹配id pwd
//                if (StringUtils.isNotBlank((String) params.get("title"))) {
//                    predicates.add(criteriaBuilder.equal(root.get("title"), params.get("title")));
//                }
//
//                //模糊搜索 name
//                if (StringUtils.isNotBlank((String) params.get("description"))) {
//                    predicates.add(criteriaBuilder.like(root.get("description"), "%" + params.get("description") + "%"));
//                }
//
//                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
//            }
//        };

        System.out.println(params);
        System.out.println(params.get("page"));
        Integer pageNo = (Integer) params.get("page");
        Integer pageSize = (Integer) params.get("limit");

        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        PageRequest pageable = PageRequest.of(pageNo, pageSize);

        List<JpaEntity> all = jpaMapper.findAll(sort);

        return new PageUtils(all, all.size(), pageNo, pageSize);
    }


}
